<?php

namespace App\Domain\CustomerManagement\Controllers;

use App\Domain\Customer\RpcService\CustomerInterface;
use App\Domain\FamilyMember\RpcService\FamilyMemberInterface;
use App\Http\Controllers\Controller;
use App\Supports\Constant\PaginationConst;
use App\Supports\Formatter\ResponseFormatter;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;

class CustomerManagementController extends Controller
{
    public function customerList(Request $request, CustomerInterface $customer)
    {
        $params = [
            'name' => $request['name'],
            'phone' => $request['phone'],
            'gender' => $request['gender'],
            'start_time' => $request['start_time'],
            'end_time' => $request['end_time'],
            'currentPage' => $request->input('currentPage', 1),
            'pageSize' => $request->input('pageSize', PaginationConst::PAGE_SIZE),
        ];
        $customerPaginator = $customer->list($params);
        $items = array_map(function ($val) {
            $val['id'] = strval($val['id']);
            return $val;
        }, $customerPaginator->items());
        $customerPaginator = new LengthAwarePaginator($customerPaginator->items(), $customerPaginator->total(), $customerPaginator->perPage(), $customerPaginator->currentPage());
        //格式化
        $keyMap = [
            'name' => 'name',
            'phone' => 'phone',
            'gender' => 'gender',
            'region' => '',
            'where' => 'where',
            'create_time' => 'create_time',
            'wx_id' => 'wx_id',
            'state' => 'state',
        ];
        return $this->success(ResponseFormatter::formatterPaginator($customerPaginator, $keyMap));
    }

    public function getFamilyMember(Request $request, FamilyMemberInterface $familyMember)
    {
        $data = $familyMember->getlist($request['houseHolder_id']);
        return $this->success($data);
    }
}
